package com.yl.chainStore.services.impl

import com.baomidou.mybatisplus.core.toolkit.Wrappers
import com.yl.chainStore.entity.system.CompanyDictionary
import com.yl.chainStore.entity.system.CompanyDictionaryDetail
import com.yl.chainStore.mapper.system.CompanyDictionaryDetailMapper
import com.yl.chainStore.mapper.system.CompanyDictionaryMapper
import com.yl.chainStore.pojo.vo.company.CompanyDictionaryDetailUpdateVo
import com.yl.chainStore.services.ICompanyDictionaryService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service

/**
 * @description :公司数据字典实现曾
 * @author :吴枫
 * @create :2021-07-06 14:35:00
 */
@Service
class CompanyDictionaryServiceImpl(
	private val companyDictionaryMapper: CompanyDictionaryMapper,
	private val companyDictionaryDetailMapper: CompanyDictionaryDetailMapper
) : ICompanyDictionaryService {

	companion object {
		private val log = LoggerFactory.getLogger(ICompanyDictionaryService::class.java)
	}

	override fun updateDictionary(dict: CompanyDictionary): ResponseBody {
		log.info("数据字典==> 修改字典内容 dictionary：{}", dict)
		val selectById =
			companyDictionaryMapper.selectById(dict.id) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND)
		selectById.groupName = dict.groupName
		selectById.description = dict.description
		selectById.flag = dict.flag
		companyDictionaryMapper.updateById(selectById)
		return returnSuccess()
	}

	override fun deleteDictionary(id: Long): ResponseBody {
		log.info("数据字典==> 删除字典 id：{}", id)
		companyDictionaryMapper.selectById(id) ?: return returnError(ErrorCode.DATA_IS_NOT_FIND, "没有这条数据字典记录")
		companyDictionaryMapper.update(
			null, Wrappers.update<CompanyDictionary>()
				.set("enabled", 0)
				.eq("id", id)
		)
		return returnSuccess()
	}

	override fun selectDictionaryById(id: Long): ResponseBody {
		log.info("数据字典==> 查询信息 id：{}", id)
		val result = companyDictionaryMapper.selectById(id)
		return returnSuccess(result)
	}

	override fun addDetail(detail: CompanyDictionaryDetail): ResponseBody {
		log.info("数据字典==> 字典添加一个值 detail：{}", detail)
		if(detail.itemValue == "0"){
			companyDictionaryDetailMapper.insert(detail)
			detail.itemValue = detail.id.toString()
			companyDictionaryDetailMapper.updateById(detail)
		}else{
			companyDictionaryDetailMapper.insert(detail)
		}

		return returnSuccess()
	}

	override fun delDetailByDetailId(detailId: Long): ResponseBody {
		log.info("数据字典==> 字典删除一个值 detailId：{}", detailId)
		val detail = companyDictionaryDetailMapper.selectById(detailId)?: return returnSuccess()
		detail.enabled = 0
		companyDictionaryDetailMapper.updateById(detail)
		return returnSuccess()
	}

	override fun updateDetail(vo: CompanyDictionaryDetailUpdateVo):ResponseBody{
		log.info("数据字典==> 字典删除一个值 detail：{}", vo)
		val dict = companyDictionaryDetailMapper.selectById(vo.id)?: return returnError(ErrorCode.DATA_IS_NOT_FIND,"未找到数据请刷新重试")
		dict.itemName = vo.itemName
		if(vo.orderNum != null){
			dict.orderNum = vo.orderNum!!
		}
		companyDictionaryDetailMapper.updateById(dict)
		return returnSuccess()
	}

	override fun getCompanyDetailList(distId: Long, companyId: Long,subCompanyId:Long): List<CompanyDictionaryDetail> {
		log.info("数据字典==> 获取详情 distId：{}，companyId：{}", distId, companyId)
		val wrapper = Wrappers.query<CompanyDictionaryDetail?>()
			.eq("dist_id", distId)
			.eq("enabled", 1)
			.eq("company_id", companyId)
			.eq("sub_company_id",subCompanyId)
			.orderByAsc("order_num")

		return companyDictionaryDetailMapper.selectList(
			wrapper
		)
	}


	override fun getCompanyDetailListToMap(dictId: Long, companyId: Long,subCompanyId:Long): List<Map<String, Any>> {
		log.info("数据字典==> 获取详情列表 map类型 dictId：{}，companyId：{}", dictId, companyId)
		val companyDetailList = getCompanyDetailList(dictId, companyId,subCompanyId)
		val list = mutableListOf<Map<String, Any>>()

		for (detail in companyDetailList) {
			val map = mutableMapOf<String, Any>()
			map["id"] = detail.itemValue
			map["name"] = detail.itemName
			list.add(map)
		}
		return list
	}

	override fun getCompanyDetailListToMapIdName(dictId: Long, companyId: Long,subCompanyId:Long): List<Map<String, Any>> {
		log.info("数据字典==> 获取详情列表 map类型 dictId：{}，companyId：{}", dictId, companyId)
		val companyDetailList = getCompanyDetailList(dictId, companyId, subCompanyId)
		val list = mutableListOf<Map<String, Any>>()

		for (detail in companyDetailList) {
			val map = mutableMapOf<String, Any>()
			map["id"] = detail.id
			map["name"] = detail.itemName
			list.add(map)
		}
		return list
	}

	override fun getDetail(detailId: Long): CompanyDictionaryDetail? {
		log.info("数据字典==> 获取详情数据 detailId：{}", detailId)
		return companyDictionaryDetailMapper.selectById(detailId)
	}


}
